<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-sys__user">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
		  <el-form-item>
		    <el-input size="mini" v-model="dataForm.account" :placeholder="$t('user.account')" clearable></el-input>
		  </el-form-item>
        <el-form-item>
          <el-input size="mini" v-model="dataForm.username" :placeholder="$t('user.username')" clearable></el-input>
        </el-form-item>
		<el-form-item>
		  <el-input size="mini" v-model="dataForm.phone" :placeholder="$t('phone')" clearable></el-input>
		</el-form-item>
       <el-form-item>
         <el-select size="mini" v-model="dataForm.status" clearable  :placeholder="$t('status')" >
             <el-option :key="1":label="$t('status1')":value="1"> </el-option>
       	  <el-option :key="0":label="$t('status0')":value="0"> </el-option>
           </el-select>
       </el-form-item>
	   <el-form-item>
	     <ren-dept-tree  v-model="dataForm.deptId" :placeholder="$t('dept.title')" :query="true"></ren-dept-tree>
	   </el-form-item>
	   <el-form-item>
		   <el-select  v-model="dataForm.roleId" size="mini" :placeholder="$t('user.roleName')" @change="sectionValue($event)" clearable>
		       <el-option v-for="item in roleList" :key="item.id" :label="item.roleName" :value="item.id"> </el-option>
		   </el-select>
	   </el-form-item>
	   
	   
        <el-form-item>
          <el-button size="mini" @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" v-if="$hasPermission('sys_user:add')" type="primary" @click="addHandle()">{{ $t('add') }}</el-button>
        </el-form-item>
        <!-- <el-form-item>
          <el-button v-if="$hasPermission('sys_user:del')" type="danger" @click="delHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item> -->
        <!-- <el-form-item>
          <el-button v-if="$hasPermission('sys_user:export')" type="info" @click="exportHandle()">{{ $t('export') }}</el-button>
        </el-form-item> -->
      </el-form>
	  
	  
	  
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        @sort-change="dataListSortChangeHandle"
        style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="account" :label="$t('user.account')"  header-align="center" align="center"></el-table-column>
		<el-table-column prop="username" :label="$t('user.username')"  header-align="center" align="center"></el-table-column>
		<el-table-column prop="phone" :label="$t('phone')" header-align="center" align="center"></el-table-column>
		<el-table-column prop="roleName" :label="$t('user.roleName')"  header-align="center" align="center"></el-table-column>
        <el-table-column prop="deptName" :label="$t('user.deptName')" header-align="center" align="center"></el-table-column>
		<el-table-column prop="companyName" :label="$t('user.companyName')" header-align="center" align="center"></el-table-column>
		<el-table-column prop="postName" :label="$t('user.postId')" header-align="center" align="center"></el-table-column>
		
		
		
		<el-table-column v-if="!$hasPermission('sys_user:modifyStatus')" prop="status" :label="$t('user.status')"  min-width="30px" header-align="center" align="center">
		  <template slot-scope="scope">
		    <el-tag type="danger" v-if="scope.row.status === 0">{{ $t('status0') }}</el-tag>
		    <el-tag v-if="scope.row.status === 1">{{ $t('status1') }}</el-tag>
		  </template>
		</el-table-column>
		
        <el-table-column v-if="$hasPermission('sys_user:modifyStatus')"  prop="status" :label="$t('user.status')"  min-width="40px" header-align="center" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="changeStatus(scope.row)"
              v-model="scope.row.status === 1"
              size="mini"
              active-color="#7FAAF9"
              inactive-color="#999999"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" :label="$t('user.updateTime')"  header-align="center" align="center" width="180"></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('sys_user:modify')" type="text" size="small" @click="modifyHandle(scope.row,null)">{{ $t('update') }}</el-button>
			<el-button v-if="$hasPermission('sys_user:resetPwd')" type="text" size="small" @click="resetPwdShowDialog(scope.row)">{{ $t('resetPwd') }}</el-button>
            <el-button v-if="$hasPermission('sys_user:del')" type="text" size="small" @click="delHandle(scope.row.id)">{{ $t('delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="pageNo"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
	  
	  <!-- 重置密码 -->
	  <el-dialog :title="$t('prompt.title')" :visible.sync="showResetPwd" width="300px">
	    <el-form :model="resetPwdDataForm">
			<el-form-item :label="'请输入 '+ resetPwdDataForm.acc+' 的密码'" >
			</el-form-item>
			<el-form-item  >
				<el-input v-model="resetPwdDataForm.pwd" type="password" autocomplete="off"></el-input>
			</el-form-item>
	    </el-form>
	    <div slot="footer" class="dialog-footer">
	      <el-button @click="showResetPwd = false">取 消</el-button>
	      <el-button type="primary" @click="doResetPwd()">确 定</el-button>
	    </div>
	  </el-dialog>
	  
    </div>
  </el-card>
  
  
  
  
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './user-add-or-update'
import { setMd5 } from '@/utils'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/pc/systemUser/list',
        getDataListIsPage: true,
        deleteURL: '/pc/systemUser/del',
        // deleteIsBatch: true
      },
	  roleList: [],
	  
	  showResetPwd: false,
	  resetPwdDataForm: {
		  pwd: null,
		  acc: '张三'
	  },
      dataForm: {
        username: '',
        account: '',
		phone: '',
        status: '',
		roleId: null
      }
    }
  },
  mounted() {
      this.getRoleList()
	  // this.initHasModiyStatus()
  },
  components: {
    AddOrUpdate
  },
  methods:{
	  getRoleList(){
		  let params = {
		      pageNo: 1,
		      pageSize: 9999,
		  }
		  this.$http.get(`/pc/role/list`,params).then(({ data: res }) => {
		      if(res.code === 200){
				this.roleList = res.rows;
		      }
		  })
	  },
	  sectionValue(id){
		  this.dataForm.roleId = id;
	  },
	  changeStatus(obj){
		  let params = {
		      id: obj.id,
		      status: obj.status === 1 ? 0 : 1,
		  }
		  this.$http.post(`/pc/systemUser/modifyStatus`,params).then(({ data: res }) => {
		      if(res.code === 200){
					obj.status = params.status;
		  			this.$message({type: 'success', message: '修改成功'})
		      }else{
				  this.$message.error(res.msg);
			  }
		  })
	  },
	  doResetPwd() {
		  let params = {
		      id: this.resetPwdDataForm.id,
		      newPassword: setMd5(this.resetPwdDataForm.pwd)
		  }
		  this.showResetPwd = false;
		  this.$http.post(`/pc/systemUser/resetPwd`,params).then(({ data: res }) => {
		      if(res.code === 200){
		  			this.$message({type: 'success', message: '修改成功'})
		      }else{
				  this.$message.error(res.msg);
			  }
			  this.resetPwdDataForm.acc = '';
			  this.resetPwdDataForm.id = null;
			  this.resetPwdDataForm.pwd = null;
		  })
	  },
	  resetPwdShowDialog(obj) {
		  this.showResetPwd = true;
		  this.resetPwdDataForm.acc = obj.username;
		  this.resetPwdDataForm.id = obj.id;
		  this.resetPwdDataForm.pwd = null;
	  }
  }
}
</script>
